package com.kw.controller;

import com.kw.dao.JdbcImpl;
import com.kw.dao.Jdbcdao;
import com.kw.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class kwController {
    //登录界面
    @RequestMapping("/login")
    public String toLogin(@RequestParam(value = "username")String username, @RequestParam(value = "password")String password, @RequestParam(defaultValue = "off",value = "remeber")String remeber,@CookieValue(defaultValue = "off",value = "remeberMe") String cookies, HttpServletResponse response){
        System.out.println("进行登陆");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println(remeber);
        //优先查看是否有Cookie
        if("true".equals(cookies)){
            System.out.println("记住账号成功,直接登陆");
            return "forward:login.jsp";
        }
        //记住我的情况下,才会去创建Cookie
        if("on".equals(remeber)){
            if("admin".equals(username) && "admin".equals(password)) {
                Cookie cookie = new Cookie("remeberMe", "true");
                response.addCookie(cookie);
                return "forward:login.jsp";
            }else if("xiaxing".equals(username) && "123456".equals(password)) {
                Cookie cookie = new Cookie("remeberMe", "true");
                response.addCookie(cookie);
                return "forward:login.jsp";
            }else{
                return "redirect:loginfail.jsp";
            }
        }else if("off".equals(remeber)){
            System.out.println("记住我");
            if("admin".equals(username) && "admin".equals(password)) {
                return "forward:login.jsp";
                //response.sendRedirect("success.jsp");
            }else if("kangwei".equals(username) && "123456".equals(password)) {
                return "forward:login.jsp";
            }else{
                return "redirect:loginfail.jsp";
            }
        }
        return "redirect:loginfail.jsp";
    }
    //增加用户
    @RequestMapping("/userAdd")
    public String userAdd(@RequestParam(value = "username")String username,@RequestParam(value = "password")String password,@RequestParam(value = "sex")String sex,@RequestParam(defaultValue = "off",value = "likes")String[] likes,@RequestParam(value = "group")String group){
        System.out.println("进行创建用户");
        //如果有信息没填写的话,就重新填写
        System.out.println("username = " + username);
        if("".equals(username) || "".equals(password) ||"".equals(sex) ||"".equals(likes)){
            return "redirect:usererror.jsp";
        }else{
            for(String s : likes){
                System.out.println(s);
            }
            ArrayList<String> list = new ArrayList<String>();
            list.add("");
            for(String s : likes){
                list.set(0,(list.get(0) + s + "#"));
            }
            User user = new User(username,password,sex,list,group);
            Jdbcdao jdbcdao = new JdbcImpl();
            int i = jdbcdao.addUser(user);
            if(i == 1){
                return "redirect:success.jsp";
            }else{
                return "redirect:fail.jsp";
            }
        }
    }
    //删除用户
    @RequestMapping("/userDel")
    public String userAdd(@RequestParam(value = "id")String id){
        //如果有信息没填写的话,就重新填写
        if("".equals(id)){
            return "redirect:usererror.jsp";
        }else{
            Jdbcdao jdbcdao = new JdbcImpl();
            int i = jdbcdao.delUser(Integer.parseInt(id));
            if(i == 1){
                return "redirect:success.jsp";
            }else{
                return "redirect:fail.jsp";
            }
        }
    }
    //修改用户
    @RequestMapping("/userUpdate")
    public String userUpdate(@RequestParam(value = "username")String username,@RequestParam(value = "password")String password,@RequestParam(value = "sex")String sex,@RequestParam(defaultValue = "off",value = "likes")String[] likes,@RequestParam(value = "group")String group,@RequestParam(value = "id")String id){
        System.out.println("进行更新用户");
        //如果有信息没填写的话,就重新填写
        if("off".equals(username) ||"off".equals(password) ||"off".equals(sex) ||"off".equals(likes)||"".equals(id)){
            return "redirect:usererror.jsp";
        }else{
            ArrayList<String> list = new ArrayList<String>();
            list.add("");
            for(String s : likes){
                list.set(0,(list.get(0) + s + "#"));
            }
            User user = new User(Integer.parseInt(id),username,password,sex,list,group);
            Jdbcdao jdbcdao = new JdbcImpl();
            int i = jdbcdao.updateUser(user);
            if(i == 1){
                return "redirect:success.jsp";
            }else{
                return "redirect:fail.jsp";
            }
        }
    }
    //查询全部用户
    @RequestMapping("/selectAll")
    public ModelAndView selectAll(ModelAndView modelAndView){
        Jdbcdao jdbcdao = new JdbcImpl();
        ArrayList<User> users = jdbcdao.selectAll();
        modelAndView.addObject("users",users);
        modelAndView.addObject("time",new Date());
        modelAndView.setViewName("login");
        return modelAndView;
    }
}
